package logic

import (
	"context"
	"gorm.io/gorm"
	"shp/models/mysql"
	"shp/shprpc/internal/svc"
	"shp/shprpc/shprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductUpdateLogic {
	return &ProductUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductUpdateLogic) ProductUpdate(in *shprpc.ProductUpdateRequest) (*shprpc.ProductUpdateResponse, error) {
	// todo: add your logic here and delete this line
	product := mysql.Product{
		Model:  gorm.Model{ID: uint(in.ProductId)},
		UserId: in.UserId,
		Title:  in.Title,
		Image:  in.Image,
		Stock:  in.Stock,
		Price:  float64(in.Price),
	}
	mysql.DB.Debug().Updates(&product)

	return &shprpc.ProductUpdateResponse{
		ProductId: int64(product.ID),
	}, nil
}
